#!/bin/bash

################################################################################
#                                                                              #
#                    KWRT OpenWrt 软件源配置更新脚本                          #
#                                                                              #
#  功能：自动更新 /etc/opkg/distfeeds.conf 文件，配置KWRT官方软件源          #
#  版本：24.10 (aarch64_cortex-a53)                                           #
#                                                                              #
################################################################################

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 显示Banner
show_banner() {
    echo -e "${GREEN}"
    echo "================================================================================"
    echo "                       KWRT OpenWrt 软件源配置更新脚本                        "
    echo "================================================================================"
    echo -e "${NC}"
    echo -e "${YELLOW}脚本功能说明：${NC}"
    echo "  1. 自动备份现有的 /etc/opkg/distfeeds.conf 配置文件"
    echo "  2. 将软件源更新为 KWRT 官方镜像源 (dl.openwrt.ai)"
    echo "  3. 配置版本：24.10 (适用于 MediaTek Filogic / aarch64_cortex-a53)"
    echo "  4. 可选择是否立即更新软件包列表"
    echo "================================================================================"
    echo ""
}

# 备份原配置文件
backup_config() {
    local config_file="/etc/opkg/distfeeds.conf"
    local backup_file="/etc/opkg/distfeeds.conf.bak.$(date +%Y%m%d_%H%M%S)"
    
    if [ -f "$config_file" ]; then
        echo -e "${YELLOW}[INFO]${NC} 备份原配置文件到: $backup_file"
        cp "$config_file" "$backup_file"
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}[SUCCESS]${NC} 备份成功"
        else
            echo -e "${RED}[ERROR]${NC} 备份失败"
            exit 1
        fi
    else
        echo -e "${YELLOW}[WARN]${NC} 配置文件不存在，将创建新文件"
    fi
}

# 更新配置文件
update_feeds() {
    local config_file="/etc/opkg/distfeeds.conf"
    
    echo -e "${YELLOW}[INFO]${NC} 更新opkg源配置..."
    
    cat > "$config_file" << 'EOF'
src/gz kwrt_core https://dl.openwrt.ai/releases/24.10/targets/mediatek/filogic/6.6.92
src/gz kwrt_base https://dl.openwrt.ai/releases/24.10/packages/aarch64_cortex-a53/base
src/gz kwrt_packages https://dl.openwrt.ai/releases/24.10/packages/aarch64_cortex-a53/packages
src/gz kwrt_luci https://dl.openwrt.ai/releases/24.10/packages/aarch64_cortex-a53/luci
src/gz kwrt_routing https://dl.openwrt.ai/releases/24.10/packages/aarch64_cortex-a53/routing
src/gz kwrt_kiddin9 https://dl.openwrt.ai/releases/24.10/packages/aarch64_cortex-a53/kiddin9
EOF
    
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}[SUCCESS]${NC} 配置文件更新成功"
        echo ""
        echo -e "${YELLOW}[INFO]${NC} 新的配置内容："
        echo "================================================================================"
        cat "$config_file"
        echo "================================================================================"
        return 0
    else
        echo -e "${RED}[ERROR]${NC} 配置文件更新失败"
        exit 1
    fi
}

# 更新opkg包列表
update_opkg() {
    echo ""
    echo -e "${YELLOW}[INFO]${NC} 正在自动更新opkg包列表..."
    opkg update
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}[SUCCESS]${NC} opkg包列表更新成功"
    else
        echo -e "${RED}[ERROR]${NC} opkg包列表更新失败"
        echo -e "${YELLOW}[INFO]${NC} 您可以稍后手动运行: opkg update"
    fi
}

# 主函数
main() {
    show_banner
    
    # 检查是否为root用户
    if [ "$EUID" -ne 0 ]; then
        echo -e "${RED}[ERROR]${NC} 请使用root权限运行此脚本"
        exit 1
    fi
    
    # 备份配置
    backup_config
    
    # 更新配置
    update_feeds
    
    # 询问是否更新opkg
    update_opkg
    
    echo ""
    echo -e "${GREEN}================================================================================"
    echo "                              所有操作已完成！                                  "
    echo "================================================================================${NC}"
    echo ""
    echo -e "${YELLOW}已完成的操作总结：${NC}"
    echo "  ✓ 已备份原配置文件"
    echo "  ✓ 已更新 opkg 软件源配置为 KWRT 官方源"
    echo "  ✓ 配置的软件源包括：core, base, packages, luci, routing, kiddin9"
    echo ""
    echo -e "${YELLOW}提示：${NC}您现在可以使用以下命令安装软件包："
    echo "  opkg update          # 更新软件包列表"
    echo "  opkg list            # 查看可用软件包"
    echo "  opkg install <包名>  # 安装软件包"
    echo ""
}

# 执行主函数
main
